package pnu.ssm.sbmother;

import pnu.ssm.http.HttpConnectorParent;
import pnu.ssm.key.Key;
import pnu.ssm.key.SBString;
import android.app.*;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.*;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends Activity {

	public static final String TAG = "LoginActivity";
	private final int FAIL_LOGIN = 1;
	
	TextView tv_id;
	TextView tv_password;
	EditText et_id;
	EditText et_password;
	Button btn_login;
	Button btn_register;
	HttpConnectorParent connector;
	ProgressDialog dialog;
	SharedPreferences pref;
	SharedPreferences.Editor editor;
	boolean isLogin;

	Handler mHandler = new Handler(Looper.getMainLooper()) {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case FAIL_LOGIN:
				Toast.makeText(LoginActivity.this, "로그인에 실패했습니다.", Toast.LENGTH_LONG)
						.show();
				break;
			default:
				Log.e(TAG, "handler error : " + msg.what);
				break;
			}
		};
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		buildUI();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		ActionBar actionBar = getActionBar();
		actionBar.setTitle("로그인");
		
		return super.onCreateOptionsMenu(menu);
	}

	private void buildUI() {
		isLogin = false;
		pref = getSharedPreferences(Key.prefKey, MODE_PRIVATE);
		editor = pref.edit();
		isLogin = pref.getBoolean(Key.isLogin, false);
		
		dialog = new ProgressDialog(this);
		dialog.setMessage(SBString.WAIT);
		dialog.setIndeterminate(true);
		dialog.setCancelable(true);

		tv_id = (TextView) findViewById(R.id.tv_parent_id);
		tv_password = (TextView) findViewById(R.id.tv_password);
		et_id = (EditText) findViewById(R.id.et_parent_id);
		et_password = (EditText) findViewById(R.id.et_password);
		btn_login = (Button) findViewById(R.id.btn_login);
		btn_register = (Button) findViewById(R.id.btn_register);
		
		connector = HttpConnectorParent.getInstance();
		btn_login.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				new LoginAsynTask(et_id.getText().toString(), et_password.getText().toString()).execute(null, null, null);
			}
		});
		btn_register.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
				startActivity(intent);
			}
		});
	}

	class LoginAsynTask extends AsyncTask<Void, Void, String> {

		String id;
		String password;

		public LoginAsynTask(String id, String password) {
			this.id = id;
			this.password = password;
		}
		
		protected void onPreExecute() {
			dialog.show();
			super.onPreExecute();
		}
		
		protected String doInBackground(Void... params) {
			//parent_num를 반환해준다.
			String result = connector.login(id, password);
			return result;
		}
		
		protected void onPostExecute(String parent_num) {
			dialog.dismiss();
			super.onPostExecute(parent_num);
			if (parent_num == null) { // fail to log in
				mHandler.sendEmptyMessage(FAIL_LOGIN);
			} else {
				editor.putString(Key.parent_num, parent_num);
				editor.putString(Key.id, et_id.getText().toString());
				editor.putString(Key.password, et_id.getText().toString());
				editor.putBoolean(Key.isLogin, true);
				editor.commit();
				
				Intent intent;
				intent = new Intent(getApplicationContext(), SelectActivity.class);
				startActivity(intent);
				finish();
			}
		}
	}

}
